//此程序还未连接到开发板

let SSID = document.getElementById("auth_user");
let PASSWARD = document.getElementById("auth_pass");
let Connect = document.querySelector("#connect");
let Connect0 = document.querySelector("#connect0");
let Donut = document.querySelector("#donut");

let check=0;
let count=0;


let WiFi_ssid = "/WiFi-ssid"; //地址拼接
let WiFi_passward = "/WiFi-passward"; //地址拼接
let WiFi_check = "/WiFi-check"; //地址拼接

let interval;

check_connect = function () {
  console.log("check");
  // 1.创建 XMLHttpRequest 对象
  let xhr = new XMLHttpRequest();
  // 2.调用open函数，指定请求方式与URL地址
  xhr.open("GET", WiFi_check);
  // 3.调用send函数，发起Ajax请求
  xhr.send();
  // 4.监听 onreadystatechange 事件
  xhr.onreadystatechange = function () {
    // 4.1监听xhr对象的请求状态readyState与服务器响应的状态status
    if (xhr.readyState === 4 && xhr.status === 200) {
      // if里面的判断条件是固定写法
      // 4.2打印服务器响应回来的数据
        check=xhr.responseText
      console.log(check);
    }
  };

//判断连接状态如果ckeck返回 1 的话 那么不再转圈，跳转到主页面

if(check==1){
    Connect0.value="连  接  成  功";
    Connect0.style.display = "block";
    Connect.style.display = "none";
    Donut.style.display = "none";
}


if(count==8)//8s 之后还没有连接成功 那么就提示连接失败，点击重新连接
{
    Connect.value="连接失败，点我重新连接";
    Connect0.style.display = "none";
    Connect.style.display = "block";
    Donut.style.display = "none";

    count=8;
}




count++;
};

Connect.addEventListener("click", () => {
  Connect0.style.display = "block";
  Connect.style.display = "none";
  Donut.style.display = "inline-block";
  count=0;
    clearInterval(interval);
  interval=setInterval("check_connect()", 1000)


  let passward = PASSWARD.value;
  let ssid = SSID.value;
  console.log("ssid= " + ssid);
  console.log("passward= " + passward);

  let sendRequest = new XMLHttpRequest();
  sendRequest.open(
    "POST",
    WiFi_ssid + "?ssid=" + ssid ,
    true
  );
  sendRequest.send(null);
  sendRequest.onreadystatechange = function () {
    if (sendRequest.readyState === 4 && sendRequest.status === 200) {
      console.log(sendRequest.responseText);
    }
  };

  let sendRequest1 = new XMLHttpRequest();
  sendRequest1.open(
    "POST",
    WiFi_passward + "?passward=" + passward ,
    true
  );
  sendRequest1.send(null);
  sendRequest1.onreadystatechange = function () {
    if (sendRequest1.readyState === 4 && sendRequest1.status === 200) {
      console.log(sendRequest1.responseText);
    }
  };


});
